var dialogId;
var nodeIds;
var targetId;
var targetType;
var userAuthColSize;	//权限类型数量
var rcdDataType;	//条目类型
var authCount = 0;

$(function(){
	//默认按非电子文件查询条件显示
	$("#wjhDiv").show();
	$("#tmDiv").show();
	$("#attachNameDiv").hide();
	$("#waitSelectEntryDatagrid").datagrid("hideColumn","ATTACH_NAME");
	
	$('#entryTypeCombobox').combobox({
		onSelect : function(record){
			rcdDataType = record.value;
			if(rcdDataType == "0"){
				//电子文件
				$("#wjhDiv").hide();
				$("#tmDiv").hide();
				$("#attachNameDiv").show();
				//$("#waitSelectEntryDatagrid").datagrid("hideColumn","WJH");
				//$("#waitSelectEntryDatagrid").datagrid("hideColumn","TM");
				$("#waitSelectEntryDatagrid").datagrid("showColumn","ATTACH_NAME");
			}
			else{
				//非电子文件
				$("#wjhDiv").show();
				$("#tmDiv").show();
				$("#attachNameDiv").hide();
				$("#waitSelectEntryDatagrid").datagrid("hideColumn","ATTACH_NAME");
				//$("#waitSelectEntryDatagrid").datagrid("showColumn","WJH");
				//$("#waitSelectEntryDatagrid").datagrid("showColumn","TM");
			}
			$("#waitSelectEntryDatagrid").datagrid("options").url = "platform/UserAuthSetController/getWaitSelectEntry";
			$('#waitSelectEntryDatagrid').datagrid('load', {
				entryType : record.value,
				nodeIds : nodeIds,
				targetType : targetType,
				targetId : targetId
			});
		}
	});

});

/**
 * 将权限咧转化为复选框
 */
function formatCheckbox(value, rowData, rowIndex) {
	if(rowIndex == "0"){
		if(null != rowData.START_DATE && "" != rowData.START_DATE){
			$("#startDate").datebox("setValue", rowData.START_DATE.substring(0,10));	//开始时间
		}
		if(null != rowData.END_DATE && "" != rowData.END_DATE){
			$("#endDate").datebox("setValue", rowData.END_DATE.substring(0,10));	//结束时间
		}
	}
	var c = "";
	var colName = value.substring(0,2);		//数据中前两位用来保存列名
	if(value == (colName+"1")){
		c = "<input type='checkbox' value='"+colName+"1' id='"+rowIndex+colName+"' checked='checked' onclick='onCheckboxClick(\""+rowIndex+colName+"\",\""+rowIndex+"\",\""+colName+"\")' />";
	}
	else{
		c = "<input type='checkbox' value='"+colName+"0' id='"+rowIndex+colName+"' onclick='onCheckboxClick(\""+rowIndex+colName+"\",\""+rowIndex+"\",\""+colName+"\")' />";
	}
	return c;
}

/**
 * 复选框点击事件：点击复选框后更新对应的行记录
 * @param checkboxId
 * @param rowId
 * @param colName
 */
function onCheckboxClick(checkboxId,rowIndex,colName){
	var checkbox = document.getElementById(checkboxId);
	
	var index = colName.substring(1);	//获取复选框顺序索引
	var name = colName.substring(0,1);
	
	if(checkbox.checked){
//		var val = colName + "1";
//		var rowStr = '{ "'+colName+'": "'+val+'"}'; 
//		var row = JSON.parse(rowStr);
		//勾选当前复选框之前的所有复选框
		var rowStr = '{'; 
		for(var i = index; i > 0; i--){
			var val = name + i + "1";
			rowStr += '"' + name + i +'": "'+val+'",';
		}
		rowStr = rowStr.substring(0, rowStr.length-1) + "}";
		var row = JSON.parse(rowStr);
		
		$('#authEntryDatagrid').datagrid('updateRow',{
			index: rowIndex,
			row: row
		});
	}
	else{
//		var val = colName + "0";
//		var rowStr = '{ "'+colName+'": "'+val+'"}'; 
//		var row = JSON.parse(rowStr);
		//取消勾选当前复选框之后的所有复选框
		var rowStr = '{'; 
		for(var i = index; i <= authCount; i++){
			var val = name + i + "0";
			rowStr += '"' + name + i +'": "'+val+'",';
		}
		rowStr = rowStr.substring(0, rowStr.length-1) + "}";
		var row = JSON.parse(rowStr);
		
		$('#authEntryDatagrid').datagrid('updateRow',{
			index: rowIndex,
			row: row
		});
	}
}

/**
 * 添加：将待选择条目添加到权限设置列表
 */
function addEntryToAuthEntry(){
	var checkedRows = $("#waitSelectEntryDatagrid").datagrid("getChecked");
	if(checkedRows.length > 0){
		//拼接动态咧初始值
		var checkedColStr = "";
		for(var i = 1; i <= userAuthColSize; i++){
			checkedColStr += ', "A'+i+'": "A'+i+'0"'; 
		}
		for(var i = 0; i < checkedRows.length; i++){
			var wjh = checkedRows[i].WJH == null ? "" : checkedRows[i].WJH;
			var tm = checkedRows[i].TM == null ? "" : checkedRows[i].TM;
			//为电子文件时，保存电子文件名称
			var attachName = checkedRows[i].ATTACH_NAME == null ? "" : checkedRows[i].ATTACH_NAME;
			var rowStr = '{ "ARCHIVE_CLASS_NODE_NAME": "'
					+ checkedRows[i].ARCHIVE_CLASS_NODE_NAME + '","WJH": "'
					+ wjh + '","TM": "' + tm + '","ATTACH_NAME": "' + attachName + '","TABLE_ID": "'
					+ checkedRows[i].TABLE_ID + '","RES_CODE": "'
					+ checkedRows[i].ID + '","ARCHIVE_CLASS_ID": "'
					+ checkedRows[i].ARCHIVE_CLASS_ID + '"' + checkedColStr
					+ ',"RCD_DATA_TYPE":"' + rcdDataType + '"}'; 
			var row = JSON.parse(rowStr);
			var rowIndex = $('#authEntryDatagrid').datagrid("getRows").length;
			$('#authEntryDatagrid').datagrid('insertRow',{
				index: rowIndex,
				row: row
			});
		}
		
		//添加条目后删除对应的待选择条目
		for(var i = 0; i < checkedRows.length; i++){
			var index = $("#waitSelectEntryDatagrid").datagrid("getRowIndex",checkedRows[i]);
			$("#waitSelectEntryDatagrid").datagrid("deleteRow",index);
		}
	}
	else{
		//commonLang.tip.alert替换'提示',commonLang.optip.add.selectEntry替换'请选择条目！'
		$.messager.alert(commonLang.tip.alert, commonLang.optip.add.selectEntry, 'warning');
	}
}

/**
 * 保存权限设置条目
 */
function saveAuthEntry(){
	var allChangeRows = $("#authEntryDatagrid").datagrid("getRows");
	var msg = "";
	for(var i = 0; i < allChangeRows.length; i++){
		//列的值为列名+值
		if(allChangeRows[i].A1 != "A11"){
			msg = "请选择操作设置";
			break;
		}
	}
	if(msg != ""){
		//commonLang.tip.alert替换'提示',commonLang.optip.save.selectOperateSet替换'请选择操作设置！'
		$.messager.alert(commonLang.tip.alert, commonLang.optip.save.selectOperateSet, 'warning');
		return;
	}
	var startDate = $("#startDate").datebox("getValue");	//开始时间
	var endDate = $("#endDate").datebox("getValue");		//结束时间
	if(allChangeRows.length > 0){
		MaskUtil.mask();
		var datas = JSON.stringify(allChangeRows);
		$.ajax({
			url : 'platform/UserAuthSetController/saveAuthEntry',
			data : {
				targetType : targetType,
				targetId : targetId,
				rcdDataType : rcdDataType,
				datas : datas,
				startDate : startDate,
				endDate : endDate,
				nodeIds : nodeIds      //节点Id
				},
			type : 'post',
			dataType : 'json',
			success : function(result) {
				MaskUtil.unmask();
				if (result.flag == "0") {
					$('#authEntryDatagrid').datagrid('load');
					
					$.messager.show({
						title : '提示',
						//commonLang.optip.save.entryAuthSetSuccess替换"条目权限设置成功！"
						msg : commonLang.optip.save.entryAuthSetSuccess
					});
				}else{
					$.messager.show({
						title : '提示',
						//commonLang.optip.save.entryAuthSetFail替换"条目权限设置失败！"
						msg : commonLang.optip.save.entryAuthSetFail
					});
				}
			}
		});
	}
	else{
		//commonLang.tip.alert替换'提示',commonLang.optip.save.noChange替换'没有修改任何内容！'
		$.messager.alert(commonLang.tip.alert, commonLang.optip.save.noChange, 'warning');
	}
}

/**
 * 删除条目权限设置
 */
function delAuthEntry(){
	var rows = $('#authEntryDatagrid').datagrid('getChecked');
	var ids = [];
	if (rows.length > 0) {
		//commonLang.tip.confirm替换'请确认',commonLang.optip.del.deleteSelectData替换'您确定要删除当前所选的数据？'
		$.messager.confirm(commonLang.tip.confirm, commonLang.optip.del.deleteSelectData,
			function(b) {
				if (b) {
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].ID);
					}
				$.ajax({
					url : 'platform/UserAuthSetController/delAuthEntry',
					data : {
						ids : ids.join(','),
						nodeIds : nodeIds      //节点Id
					},
					type : 'post',
					dataType : 'json',
					success : function(result) {
						if (result.flag == "0") {
							for(var i = 0; i < rows.length; i++){
								var index = $("#authEntryDatagrid").datagrid("getRowIndex",rows[i]);
								$("#authEntryDatagrid").datagrid("deleteRow",index);
							}
							//删除后重新刷新每一行，用户更新行index
							var allRow = $('#authEntryDatagrid').datagrid('getRows');
							for(var i = 0; i < allRow.length; i++){
								var index = $("#authEntryDatagrid").datagrid("getRowIndex",allRow[i]);
								$("#authEntryDatagrid").datagrid("refreshRow",index);
							}
							//刷新左侧条目权限数据
							$("#waitSelectEntryDatagrid").datagrid("options").url = "platform/UserAuthSetController/getWaitSelectEntry";
							$('#waitSelectEntryDatagrid').datagrid('load', {
								entryType : rcdDataType,
								nodeIds : nodeIds,
								targetId : targetId
							});
							
							$.messager.show({
								title : '提示',
								//commonLang.optip.del.success替换"删除成功!"
								msg : commonLang.optip.del.success,
								timeout:2000,  
					            showType:'slide'  
							});
						}else{
							$.messager.show({
								title : '提示',
								//commonLang.optip.del.fail替换"删除失败!"
								msg : commonLang.optip.del.fail,
								timeout:2000,  
					            showType:'slide'  
							});
						}
					}
				});
			}
		});
	} else {
		//commonLang.tip.alert替换'提示',commonLang.optip.del.selectRecord替换'请选择要删除的记录！'
		$.messager.alert(commonLang.tip.alert, commonLang.optip.del.selectRecord, 'warning');
	}
}

/**
 * 返回
 */
function closeWindow(){
	if (parent != null && parent.$('#' + dialogId) != null) {
		parent.$('#' + dialogId).dialog('close');
	}
}

/**
 * 查询
 */
function searchData(){
	var json = serializeObject($('#formEntrySearch').form());
//	$("#waitSelectEntryDatagrid").datagrid("options").url = "platform/MyBrowseController/getBrowseRecord";
	$('#waitSelectEntryDatagrid').datagrid('load', {
		param : JSON.stringify(json),
		entryType : rcdDataType,
		nodeIds : nodeIds,
		targetId : targetId
	});
}

/**
 * 重置
 * @returns
 */
function cancelForm(){
	$('#formEntrySearch :input').val('');
}